package com.zdy.viewpager;

import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;
import in.srain.cube.views.ptr.header.StoreHouseHeader;

import java.util.ArrayList;
import java.util.List;

import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItem;

import android.graphics.Point;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * the point list used here is taken from:
 * https://github.com/cloay/CRefreshLayout
 */

public class StoreHouseUsingPointList extends Fragment {

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.fragment_storehouse_header, null);
		  int position = FragmentPagerItem.getPosition(getArguments());
	        TextView title = (TextView) view.findViewById(R.id.item_title);
	        title.setText(String.valueOf(position));
		final ImageView imageView = (ImageView) view
				.findViewById(R.id.store_house_ptr_image);

		final PtrFrameLayout frame = (PtrFrameLayout) view
				.findViewById(R.id.store_house_ptr_frame);

		// header
		final StoreHouseHeader header = new StoreHouseHeader(getActivity());
		header.setPadding(0, 25, 0, 0);

		header.initWithPointList(getPointList());

		frame.setDurationToCloseHeader(1000);
		frame.setHeaderView(header);
		frame.addPtrUIHandler(header);
		frame.postDelayed(new Runnable() {
			@Override
			public void run() {
				frame.autoRefresh(false);
			}
		}, 100);

		frame.setPtrHandler(new PtrHandler() {
			@Override
			public boolean checkCanDoRefresh(PtrFrameLayout frame,
					View content, View header) {
				return true;
			}

			@Override
			public void onRefreshBegin(final PtrFrameLayout frame) {
				frame.postDelayed(new Runnable() {
					@Override
					public void run() {
						frame.refreshComplete();
						imageView.setImageResource(R.drawable.sky);
					}
				}, 2000);
			}
		});
		return view;
	}

	private ArrayList<float[]> getPointList() {
		// this point is taken from https://github.com/cloay/CRefreshLayout
		List<Point> startPoints = new ArrayList<Point>();
		startPoints.add(new Point(240, 80));
		startPoints.add(new Point(270, 80));
		startPoints.add(new Point(265, 103));
		startPoints.add(new Point(255, 65));
		startPoints.add(new Point(275, 80));
		startPoints.add(new Point(275, 80));
		startPoints.add(new Point(302, 80));
		startPoints.add(new Point(275, 107));

		startPoints.add(new Point(320, 70));
		startPoints.add(new Point(313, 80));
		startPoints.add(new Point(330, 63));
		startPoints.add(new Point(315, 87));
		startPoints.add(new Point(330, 80));
		startPoints.add(new Point(315, 100));
		startPoints.add(new Point(330, 90));
		startPoints.add(new Point(315, 110));
		startPoints.add(new Point(345, 65));
		startPoints.add(new Point(357, 67));
		startPoints.add(new Point(363, 103));

		startPoints.add(new Point(375, 80));
		startPoints.add(new Point(375, 80));
		startPoints.add(new Point(425, 80));
		startPoints.add(new Point(380, 95));
		startPoints.add(new Point(400, 63));

		List<Point> endPoints = new ArrayList<Point>();
		endPoints.add(new Point(270, 80));
		endPoints.add(new Point(270, 110));
		endPoints.add(new Point(270, 110));
		endPoints.add(new Point(250, 110));
		endPoints.add(new Point(275, 107));
		endPoints.add(new Point(302, 80));
		endPoints.add(new Point(302, 107));
		endPoints.add(new Point(302, 107));

		endPoints.add(new Point(340, 70));
		endPoints.add(new Point(360, 80));
		endPoints.add(new Point(330, 80));
		endPoints.add(new Point(340, 87));
		endPoints.add(new Point(315, 100));
		endPoints.add(new Point(345, 98));
		endPoints.add(new Point(330, 120));
		endPoints.add(new Point(345, 108));
		endPoints.add(new Point(360, 120));
		endPoints.add(new Point(363, 75));
		endPoints.add(new Point(345, 117));

		endPoints.add(new Point(380, 95));
		endPoints.add(new Point(425, 80));
		endPoints.add(new Point(420, 95));
		endPoints.add(new Point(420, 95));
		endPoints.add(new Point(400, 120));
		ArrayList<float[]> list = new ArrayList<float[]>();

		int offsetX = Integer.MAX_VALUE;
		int offsetY = Integer.MAX_VALUE;

		for (int i = 0; i < startPoints.size(); i++) {
			offsetX = Math.min(startPoints.get(i).x, offsetX);
			offsetY = Math.min(startPoints.get(i).y, offsetY);
		}
		for (int i = 0; i < endPoints.size(); i++) {
			float[] point = new float[4];
			point[0] = startPoints.get(i).x - offsetX;
			point[1] = startPoints.get(i).y - offsetY;
			point[2] = endPoints.get(i).x - offsetX;
			point[3] = endPoints.get(i).y - offsetY;
			list.add(point);
		}
		return list;
	}
}
